package cn.keepme.ep.constants;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.controller.BaseController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.keepme.ep.answer.entity.FAnswerEntity;
import cn.keepme.ep.question.entity.FQuestionEntity;
import cn.keepme.ep.question.entity.FQuestionOptionEntity;
import cn.keepme.ep.question.entity.FTagEntity;
import cn.keepme.ep.user.entity.FUserQuizEntity;
import cn.keepme.ep.user.service.FUserQuizServiceI;

public class FGETQestionController extends BaseController {

	public static final int EMPLOYEE = 1;//员工
	public static final int STUDENT = 0;//应聘者
	
	private static FUserQuizServiceI fuserquizservice;
	
	/**
	 * 获取考试者试卷信息
	 * @param userQuizEntity 考试者信息
	 * @return 该考试者的试卷信息
	 */
	public static List<FAnswerEntity> receiveRequestCard(FUserQuizEntity userQuizEntity){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//进入应聘者试卷
		if(userQuizEntity.getType()==0){
			getQuestioncard(userQuizEntity,FGETQestionController.STUDENT);
			fuserquizservice.updateBySqlString("update f_user_quiz set active_datetime='"+format.format(new Date())
							+"' where email="+userQuizEntity.getEmail());
		}else if(userQuizEntity.getType()==0){//进入员工试卷
			getQuestioncard(userQuizEntity,FGETQestionController.EMPLOYEE);
		}
		
		List<FAnswerEntity> entity = fuserquizservice.findByQueryString("from FAnswerEntity where quiz_id="
				+userQuizEntity.getId());
		
		return entity;
	}
	
	/**
	 * 
	 * @param user 应考者
	 * @param employee 判断员工或是应聘者，0为应考者，1为员工
	 */
	public static void getQuestioncard(FUserQuizEntity user, int employee){
		
		user.setActiveDatetime(new Date());
		user.setIsActive(1);
		
		int quiz_id = user.getId();
		//获取考试者岗位
		int job = user.getJob(); 
		
		//获取该岗位所有试题(不包含性格测试)
		String sql = "from FQuestionEntity where tag_id="+job+" and parent_id=0 and type=";
		
		List<FQuestionEntity> fQuestionList = fuserquizservice.findByQueryString("from FQuestionEntity where tag_id="+job+" and parent_id!=0");//获取性格测试题
		List<FQuestionEntity> fQuestionList0 = fuserquizservice.findByQueryString(sql+0);//获取0级试题
		List<FQuestionEntity> fQuestionList1 = fuserquizservice.findByQueryString(sql+1);//获取1级试题
		List<FQuestionEntity> fQuestionList2 = fuserquizservice.findByQueryString(sql+2);//获取2级试题
		List<FQuestionEntity> fQuestionList3 = fuserquizservice.findByQueryString(sql+3);//获取3级试题
		List<FQuestionEntity> fQuestionList4 = fuserquizservice.findByQueryString(sql+4);//获取4级试题
		
		//从各等级试题按照比例随机抽取试题并打乱顺序
		List<FQuestionEntity> general = Constants.getRandom(fQuestionList0, 10);
		List<FQuestionEntity> just = Constants.getRandom(fQuestionList1, 10);
		List<FQuestionEntity> two = Constants.getRandom(fQuestionList2, 5);
		List<FQuestionEntity> three = Constants.getRandom(fQuestionList3, 3);
		List<FQuestionEntity> fourth = Constants.getRandom(fQuestionList4, 2);
		
		if(employee==FGETQestionController.STUDENT){//获取一道性格测试试题
			List<FQuestionEntity> nature = Constants.getRandom(fQuestionList, 1);
			setAnswerTable(nature, quiz_id);
		}
		
		//按照试题的等级顺序将试题依次填入试卷
		setAnswerTable(general, quiz_id);
		setAnswerTable(just, quiz_id);
		setAnswerTable(two, quiz_id);
		setAnswerTable(three, quiz_id);
		setAnswerTable(fourth, quiz_id);
		
	}
	
	/**
	 * 将抽取到的试题放入该考试者的试卷
	 * @param list 试题集合
	 * @param id 考试者id
	 */
	public static void setAnswerTable(List<FQuestionEntity> list, int id){
		
		FAnswerEntity entity = null;
		//获取该考生试卷是否已存在
		List<FAnswerEntity> falist = fuserquizservice.findByQueryString(
				"select * from f_answer where quiz_id="+id);
		
		int size = falist.size();
		for(int i=0;i<list.size();i++){
			entity = new FAnswerEntity();
			
			entity.setQuizId(id);
			entity.setQuestionId(list.get(i).getId());
			
			//如果试卷不存在，则插入的首道题目序号为1,后插入的题目一次按照顺利排列
			if(size==0){
				entity.setSort(1);
			}else{
				entity.setSort(size+i+1);
			}
			
			fuserquizservice.save(entity);
		}
		
	}
	
	/**
	 * 获取当前考题
	 * @param userQuizEntity 应考者身份信息
	 * @param aId 当前请求的试题在试卷中的序号
	 * @return
	 */
	public Map getNextQuestionCard(FUserQuizEntity userQuizEntity, int aId){
		//获取当前考试者的试卷信息
		List<FAnswerEntity> AnswerEntity = fuserquizservice.findByQueryString("from FAnswerEntity where quiz_id="
				+userQuizEntity.getId()+" and sort="+aId);
		//获取当前试题标题信息
		List<FQuestionEntity>  fQuestion = fuserquizservice.findByQueryString("from FQuestionEntity where id="
				+AnswerEntity.get(0).getQuestionId());
		//获取当前试题选项信息集合
		List<FQuestionOptionEntity>  fQuestionOPtion = fuserquizservice.findByQueryString("from FQuestionOptionEntity where question_id="
				+AnswerEntity.get(0).getQuestionId());
		//获取当前试题的岗位信息
		List<FTagEntity>  fTag = fuserquizservice.findByQueryString("from FTagEntity where id="+fQuestion.get(0).getTagId());
		
		Map resultMap = new HashMap();
		Map map = null;
		
		//获取试题类型：0单选、1多选、2简答、3上传
		int type = fQuestion.get(0).getType();
		//获取试题父ID:0为该试题单个试题，没有子题，否则为题组
		int parentId = fQuestion.get(0).getParentId();
		
		resultMap.put("id", AnswerEntity.get(0).getId());
		resultMap.put("sort", AnswerEntity.get(0).getSort());
		resultMap.put("title", fQuestion.get(0).getTitle());
		resultMap.put("type", type);
		resultMap.put("tagId", fQuestion.get(0).getTagId());
		resultMap.put("tagName", fTag.get(0).getName());
		resultMap.put("answer", null);
		
		if(type==2||type==3){
			resultMap.put("optionList", null);
			
			if(parentId==0){
				resultMap.put("_children", null);
			}else{//如果有父id，则递归查询
				
				resultMap.put("_children", getParentQuestions(resultMap, parentId));
			}
		}else{
			List<Map> optionList = new ArrayList<Map>();
			for(FQuestionOptionEntity option:Constants.getRandomOptions(fQuestionOPtion, fQuestionOPtion.size())){
				map = new HashMap();
				map.put("id", option.getQuestionId());
				map.put("des", option.getDes());
				
				optionList.add(map);
			}
			
			resultMap.put("optionList", optionList);
			
			if(parentId==0){
				resultMap.put("_children", null);
			}else{//如果有父id，则递归查询
				resultMap.put("_children", getParentQuestions(resultMap, parentId));
			}
		}
		
		return resultMap;
	}
	
	/**
	 * 获取测试题集
	 * @param m 
	 * @param parent 父id
	 * @return
	 */
	public List<Map> getParentQuestions(Map m, int parent){
		
		List<Map> map = new ArrayList<Map>();
		//获取当前试题标题信息
		List<FQuestionEntity>  fQuestion = fuserquizservice.findByQueryString("from FQuestionEntity where parent_id="
						+parent);
		Map temp = null;
		for(FQuestionEntity fq: fQuestion){
			temp = new HashMap<>();
			temp.put("id", m.get("id"));
			temp.put("sort", m.get("sort"));
			temp.put("title", fq.getTitle());
			temp.put("type", fq.getType());
			temp.put("tagId", fq.getTagId());
			temp.put("tagName", m.get("tagName"));
			temp.put("answer", m.get("answer"));
			
			//获取当前试题选项信息集合并随机打乱其顺序
			List<FQuestionOptionEntity>  fQuestionOPtion = fuserquizservice.findByQueryString("from FQuestionOptionEntity where question_id="
					+fq.getId());
			List<Map> optionList = new ArrayList<Map>();
			Map optionMap = null;
			for(FQuestionOptionEntity option:Constants.getRandomOptions(fQuestionOPtion, fQuestionOPtion.size())){
				optionMap = new HashMap();
				optionMap.put("id", option.getQuestionId());
				optionMap.put("des", option.getDes());
				
				optionList.add(optionMap);
			}
			temp.put("optionList", optionList);
			
			map.add(temp);
		}
		return map;
	}
}
